﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml;
using System.Globalization;
using System.IO;

namespace wscf.lightblue
{
	public class XmlSchemaCreator
	{
		public static string CreateSchema(string folder)
		{
			string filename = CreateFileName(folder);
			CreateSchema(filename, Path.GetFileNameWithoutExtension(filename), 
				string.Format("http://tempuri.org/{0}", Path.GetFileNameWithoutExtension(filename)));
			return filename;
		}

		public static string CreateFileName(string folder)
		{
			string filename = null;
			for(int i = 0; i < 10000; i++)
			{
				filename = Path.Combine(folder, string.Format("HelloWorld{0}.xsd", i == 0 ? "" : i.ToString()));
				if(!File.Exists(filename))
				{
					break;
				}
			}
			return filename;
		}

		public static void CreateSchema(string filePath, string schemaName, string schemaNamespace)
		{
			XmlSchema schema = new XmlSchema();
			schema.ElementFormDefault = XmlSchemaForm.Qualified;
			schema.Id = schemaName;
			XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
			ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
			ns.Add("", schemaNamespace);
			schema.Namespaces = ns;
			schema.TargetNamespace = schemaNamespace;

			XmlSchemaComplexType helloParameters = CreateType("HelloParameters", "YourName", new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"));
			schema.Items.Add(helloParameters);
			XmlSchemaComplexType helloRequestType = CreateType("HelloRequestType", "Parameters", new XmlQualifiedName("HelloParameters", schemaNamespace));
			schema.Items.Add(helloRequestType);
			XmlSchemaElement helloRequest = CreateElement("HelloRequest", new XmlQualifiedName("HelloRequestType", schemaNamespace));
			schema.Items.Add(helloRequest);

			XmlSchemaComplexType helloResults = CreateType("HelloResults", "Greeting", new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"));
			schema.Items.Add(helloResults);
			XmlSchemaComplexType helloResponseType = CreateType("HelloResponseType", "Results", new XmlQualifiedName("HelloResults", schemaNamespace));
			schema.Items.Add(helloResponseType);
			XmlSchemaElement helloResponse = CreateElement("HelloResponse", new XmlQualifiedName("HelloResponseType", schemaNamespace));
			schema.Items.Add(helloResponse);

			XmlSchemaSet set = new XmlSchemaSet();
			set.Add(schema);
			set.Compile();

			XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
			writer.Formatting = Formatting.Indented;
			writer.Indentation = 2;
			writer.WriteComment("Hello World Service XML Schema");
			writer.WriteComment("Generated by wscf.lightblue");
			schema.Write(writer);
			writer.Close();
		}

		static private XmlSchemaComplexType CreateType(string name, string elementName, XmlQualifiedName elementType)
		{
			XmlSchemaComplexType type = new XmlSchemaComplexType();
			type.Name = name;
			XmlSchemaSequence sequence = new XmlSchemaSequence();
			XmlSchemaElement element = CreateElement(elementName, elementType);
			sequence.Items.Add(element);
			type.Particle = sequence;
			return type;
		}

		static private XmlSchemaElement CreateElement(string name, XmlQualifiedName type)
		{
			XmlSchemaElement element = new XmlSchemaElement();
			element.Name = name;
			element.SchemaTypeName = type;
			return element;
		}
	}
}
